package code.rocky.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ConcurrentHashMapSafeTest {

    private static Integer COUNT;
    private static AtomicInteger Number = new AtomicInteger();
    private static List<Test> tasks;

    static {
        tasks = new ArrayList<>();
        tasks.add(new Test());
        tasks.add(new Test());
        tasks.add(new Test());
        tasks.add(new Test());
        tasks.add(new Test());
        COUNT = tasks.size();
    }

    public static void main(String[] args) {
//        for (int i = 0; i < 1000; i++) {
//            test1();
//        }
//        for (int i = 0; i < 1000; i++) {
//            test2();
//        }
//        for (int i = 0; i < 2000; i++) {
//            test3();
//        }
//        System.out.println("1000 * 5 =" + Number + " ? ");

            test4();

    }

    public static void test1() {

        HashMap<Test, Integer> map = new HashMap();
        CountDownLatch countDownLatch = new CountDownLatch(COUNT);
        for (int i = 0; i < tasks.size(); i++) {
            int finalI = i;
            new Thread(()->{
                map.put(tasks.get(finalI), finalI);
                try {
                    TimeUnit.NANOSECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            }).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (map.size() != COUNT) {
            System.out.println("=====" + map.size());
        }
    }
    public static void test4() {
        while (true) {
//            HashMap<Test, Integer> map = new HashMap();
            ConcurrentHashMap<Test, Integer> map = new ConcurrentHashMap();
            CountDownLatch countDownLatch = new CountDownLatch(COUNT);
            for (int i = 0; i < tasks.size(); i++) {
                int finalI = i;
                new Thread(() -> {
                    map.put(tasks.get(finalI), finalI);
                    try {
                        TimeUnit.NANOSECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    countDownLatch.countDown();
                }).start();
            }

            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (map.size() != COUNT) {
                System.out.println("=====" + map.size());
                break;
            }
            System.out.println("=====" + map.size());
        }
    }

    public static void test2() {

        HashMap<Test, Integer> map = new HashMap();
        CountDownLatch countDownLatch = new CountDownLatch(COUNT);
        for (int i = 0; i < tasks.size(); i++) {
            int finalI = i;
            new Thread(()->{
                map.compute(tasks.get(finalI),(key,value) -> Number.addAndGet(1));
                countDownLatch.countDown();
            }).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void test3() {

        ConcurrentHashMap<Test, Integer> map = new ConcurrentHashMap();
        CountDownLatch countDownLatch = new CountDownLatch(COUNT);
        for (int i = 0; i < tasks.size(); i++) {
            int finalI = i;
            new Thread(()->{
                map.put(tasks.get(finalI), finalI);
                try {
                    TimeUnit.NANOSECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            }).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (map.size() != COUNT) {
            System.out.println("=====" + map.size());
        }
    }

}

class Test {

    @Override
    public int hashCode() {
        return 1;
    }
}
